/*
 * @Author: your name
 * @Date: 2021-09-02 10:26:05
 * @LastEditTime: 2021-09-02 16:54:02
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \briup\5-javascript\day09\3-Date.js
 */

var date = new Date();
console.log(date); //2021-09-02T02:26:50.529Z
//getDate() 具体几号
console.log(date.getDate()); //2
//getDay() 具体星期几
console.log(date.getDay()); //4
//注意星期天的结果
var date2 = new Date("2021-09-05");
console.log(date2.getDay()); //0
//getHours  几点
console.log(date.getHours()); //11
//getMinutes 几分
console.log(date.getMinutes()); //6
//getSeconds 几秒
console.log(date.getSeconds()); //16
//getMilliseconds 毫秒
console.log(date.getMilliseconds()); //222
//getMonth 月份 0开始到11
console.log(date.getMonth() + 1); //9
//getTime
console.log(date.getTime()); //1630552334810
//getYear()  当前年份-1900
console.log(date.getYear()); //121
//valueOf() 获取毫秒 
console.log(date.valueOf()); //1630552334810
//toDateString
console.log(date.toDateString()); //Thu Sep 02 2021
//toTimeString
console.log(date.toTimeString()); //11:18:37 GMT+0800 (中国标准时间)
//toISOString
console.log(date.toISOString()); //2021-09-02T03:19:49.327Z
//toJSON
console.log(date.toJSON()); //2021-09-02T03:19:49.327Z
//toString
console.log(date.toString()); //11:18:37 GMT+0800 (中国标准时间)

//提取年份
var str = '2021-09-02';
//1. getFullYear()
var date1 = new Date();
console.log(date1.getFullYear()); //2021
//2. slice()或者substring()
console.log(str.slice(0, 4)); //2021
console.log(str.substring(0, 4)); //2021

//拼接2021-09-02 10:25:40
//date方法
var date = new Date("2021-09-02 10:25:40");
var r1 = date.getFullYear();
var r2 = date.getMonth() + 1;
var r3 = date.getDate();
var r4 = date.getHours();
var r5 = date.getMinutes();
var r6 = date.getSeconds();
console.log(r1 + "-" + "0" + r2 + "-" + "0" + r3 + " " + r4 + ":" + r5 + ":" + r6); //2021-09-02 10:25:40

//slice截取方法
var date = "2021-09-02 10:25:40";
var c1 = date.slice(0, 10);
var c2 = date.slice(11, 19);
console.log(c1 + " " + c2); //2021-09-02 10:25:40

//函数方法
function fn(date) {
    var y = date.getFullYear();
    var m = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
    var d = date.getDate().toString().padStart(2, "0");

    var hh = date.getHours().toString().padStart(2, "0");
    var mm = date.getMinutes().toString().padStart(2, "0");
    var ss = date.getSeconds().toString().padStart(2, "0");
    return y + "-" + m + "-" + d + " " + hh + ":" + mm + ":" + ss;
}

var s1 = fn(new Date());
console.log(s1); //2021-09-02 16:47:27

//第三方库方法
var moment = require("moment");
var date = new Date();
//HH:24小时制，hh：12小时制 a:pm下午几点
var result = moment(date).format("YYYY-MM-DD HH:mm:ss");
console.log(result); //2021-09-02 16:47:27

//时间戳
var date = new Date();
console.log(date.getTime()); //1630572447744

//时间戳转换成年月日制
var moment = require("moment");
var date = new Date();
var thisTime = date.getTime();
var result = moment(thisTime).format("YYYY-MM-DD HH:mm:ss");
console.log(result); //2021-09-02 16:47:27